package com.ruoyi.web.controller.module.market;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.market.domain.Level;
import com.ruoyi.market.service.ILevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 等级设置信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/market/level")
public class LevelController extends BaseController
{
    @Autowired
    private ILevelService levelService;

    /**
     * 获取等级设置列表
     */
    @PostMapping("/levelList")
    public TableDataInfo list(@RequestBody Level level, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            level.setShopId(shopId);
        }
        startPageByPost();
        List<Level> list = levelService.selectLevelList(level);
        return getDataTable(list);
    }

    /**
     * 根据等级设置编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('market:level:query')")
    @GetMapping(value = { "/", "/{levelId}" })
    public AjaxResult getInfo(@PathVariable(value = "levelId", required = false) Long levelId)
    {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(levelId))
        {
            Level sysLevel = levelService.selectLevelById(levelId);
            ajax.put(AjaxResult.DATA_TAG, sysLevel);
        }
        return ajax;
    }

    /**
     * 新增等级设置
     */
    @PreAuthorize("@ss.hasPermi('market:level:add')")
    @Log(title = "等级设置管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Level level, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            level.setShopId(shopId);
        }
        if (!levelService.checkLevelTitleUnique(level))
        {
            return error("新增'" + level.getLevelTitle() + "'失败，已存在相同数据");
        }
        level.setCreateBy(getUsername());
        return toAjax(levelService.insertLevel(level));
    }

    /**
     * 修改等级设置
     */
    @PreAuthorize("@ss.hasPermi('market:level:edit')")
    @Log(title = "等级设置管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Level level)
    {
        if (!levelService.checkLevelTitleUnique(level))
        {
            return error("修改'" + level.getLevelTitle() + "'失败，已存在相同数据");
        }
        level.setUpdateBy(getUsername());
        return toAjax(levelService.updateLevel(level));
    }

    /**
     * 删除等级设置
     */
    @PreAuthorize("@ss.hasPermi('market:level:remove')")
    @Log(title = "等级设置管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{levelIds}")
    public AjaxResult remove(@PathVariable Long[] levelIds)
    {
        return toAjax(levelService.deleteLevelByIds(levelIds));
    }
}
